package view {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import fs.message.MessageTransmitter;
	import message.GameMessage;
	import model.BitBoard;
	import model.GameData;
	import model.PiecesDefination;
	
	/**
	 * ...
	 * @author flyingSnow
	 */
	public class GameBoard extends Sprite {
		private var _gameData:GameData;
		private var _squsLayer:Sprite;
		private var _squList:Array;
		public function GameBoard() {
			drawBoard();
			addSquares();
		}
		
		private function drawBoard():void {
			var boardSize:int = Square.STANDARD_SIZE * BitBoard.BOARD_WIDTH;
		
			graphics.lineStyle(2);
			graphics.drawRect(0, 0, boardSize, boardSize);
			
			graphics.lineStyle(1);
			
			var lineX:int = Square.STANDARD_SIZE;
			var lineY:int = Square.STANDARD_SIZE;
			
			for (var i:int = 0; i < 20; i++ ) {				
				graphics.moveTo(0, lineY);
				graphics.lineTo(boardSize, lineY);
				
				graphics.moveTo(lineX, 0);
				graphics.lineTo(lineX, boardSize);
				
				lineX += Square.STANDARD_SIZE;
				lineY += Square.STANDARD_SIZE;
			}
		}
		private function addSquares():void {
			_squsLayer = new Sprite();
			addChild(_squsLayer);
			
			_squList = [];
			
			for (var i:int = 0; i < BitBoard.BOARD_WIDTH; i++ ) {
				for (var j:int = 0; j < BitBoard.BOARD_WIDTH; j++ ) {
					var squ:Square = new Square();
					squ.state = 0;
					squ.x = j * Square.STANDARD_SIZE;
					squ.y = i * Square.STANDARD_SIZE;
					squ.row = i;
					squ.column = j;
					squ.addEventListener(MouseEvent.CLICK,onSquClick);
					squ.addEventListener(MouseEvent.ROLL_OVER,onSquOver);
					
					_squsLayer.addChild(squ);
					_squList.push(squ);
				}
			}
		}
		
		private function onSquOver(e:MouseEvent):void {
			var squ:Square = e.currentTarget as Square;
			MessageTransmitter.transmitMessage(GameMessage.MOVE_ON_SQUARE, {row:squ.row,column:squ.column } );
		}
		
		private function onSquClick(e:MouseEvent):void {
			var squ:Square = e.currentTarget as Square;
			//var piece:int = _gameData.crntPlayer.crntPieceIndex;
			//var drct:int = _gameData.crntPlayer.crntDirection;
			
			var column:int = squ.column;
			var row:int = squ.row;
			
			MessageTransmitter.transmitMessage(GameMessage.CLICK_SQURE, {column:column,row:row } );
		}
		
		public function validateBoard():void {
			for (var i:int = 0; i < BitBoard.BOARD_WIDTH; i++ ) {
				for (var j:int = 0; j < BitBoard.BOARD_WIDTH; j++ ) {
					for (var p:int = 0; p < _gameData.totalPlayers; p++ ) {
						var crntSqu:Square = (_squList[i * BitBoard.BOARD_WIDTH + j] as Square);
						if (_gameData.getPlayer(p).crntPieces.getBit(i, j)) {
							crntSqu.state = 1;
							crntSqu.colorIndex = p;
						}else if ((0 == _gameData.crntPieces.getBit(i, j))){
							if(_gameData.crntPlayer.lightingPoints.getBit(i, j)) {
								crntSqu.state = 1;
								crntSqu.colorIndex = 4;
							}else {
								crntSqu.state = 0;
							}
						}
					}
				}
			}
		}
		public function get gameData():GameData { return _gameData; }
		
		public function set gameData(value:GameData):void {
			_gameData = value;
		}
		
	}

}